home *** CD-ROM | disk | FTP | other *** search
/ Teach Your Children: Road Construction Ahead / Teach Your Children: Road Construction Ahead.iso / pc / rca / road.dxr / 00011_Truck Parent.ls < prev    next >
Encoding:
Text File  |  1996-07-17  |  17.7 KB  |  493 lines

  1. property channel, cell, Xloc, Yloc, cellObject, lastMove, newMove, nextCell, nextCellObject, firstCell, startDirection, blockedCell, blockedCellObj, exitBridgeDir, invisibleButtonList, previousCell, buildingSound, status, animationScript, rotationSteps, currentState, animCounter, Xincrement, Yincrement, pixelsToMove, tempo, programmedMove, latestMove, playBack, playBackCounter
  2. global gSimObject, gGasGaugeObject, gLevelObject, gTruckObject, gMenuObject, gActorList, gCDpath, gFileSep
  3.  
  4. on birth me
  5.   set channel to the truckSprite of gSimObject
  6.   return me
  7. end
  8.  
  9. on initTruckProps me, firstTruckCell, direction
  10.   set status to #idle
  11.   set programmedMove to 0
  12.   set firstCell to firstTruckCell
  13.   set cell to firstCell
  14.   set previousCell to the startCell of gSimObject
  15.   set startDirection to direction
  16.   set newMove to startDirection
  17.   set lastMove to startDirection
  18.   set x to blockCrossings(the startCell of gSimObject, startDirection)
  19.   set cellObject to getAt(the cellList of gSimObject, cell)
  20.   set Xloc to the Xloc of cellObject
  21.   set Yloc to the Yloc of cellObject
  22.   set the startTileNum of cellObject to getProp(the partialTileList of gSimObject, startDirection)
  23.   set the startTileType of cellObject to #Road
  24.   set the tileNum of cellObject to the startTileNum of cellObject
  25.   set the tileType of cellObject to #Road
  26.   set the recordedMoveList of gSimObject to [startDirection]
  27.   set playBack to 0
  28. end
  29.  
  30. on moveTruck me, moveInput
  31.   set latestMove to moveInput
  32.   surroundStaticSprite(gSimObject, previousCell, cell)
  33.   set the locH of sprite the underTruckSprite of gSimObject to 1000
  34.   updateStage()
  35.   if the demoFlag of gSimObject <> 1 then
  36.     hideInvisibleButtons(gSimObject)
  37.   end if
  38.   surroundStaticSprite(gSimObject, cell, cell)
  39.   updateStage()
  40.   set whatType to the tileType of getAt(the cellList of gSimObject, cell + getProp(the moveList of gSimObject, latestMove))
  41.   if (whatType = #immovable) or (whatType = #Building) then
  42.     rumbleAndShake(me, lastMove, latestMove)
  43.   else
  44.     set newMove to latestMove
  45.     add(the recordedMoveList of gSimObject, newMove)
  46.     set nextCell to cell + getProp(the moveList of gSimObject, newMove)
  47.     set nextCellObject to getAt(the cellList of gSimObject, nextCell)
  48.     surroundSprite(cell, newMove)
  49.     if the demoFlag of gSimObject = 1 then
  50.       lightButton(gSimObject, newMove)
  51.     end if
  52.     fillRoadTile(newMove)
  53.     redrawCell(nextCellObject, the destinationSprite of gSimObject)
  54.     updateStage()
  55.     rotateSprite(me, lastMove, newMove)
  56.   end if
  57. end
  58.  
  59. on afterRotating me
  60.   set status to #idle
  61.   if the demoFlag of gSimObject = 1 then
  62.     unlightButton(gSimObject, newMove)
  63.   end if
  64.   if the tileType of nextCellObject <> #BG then
  65.     set programmedMove to 0
  66.     set blockedCell to nextCell
  67.     set blockedCellObj to nextCellObject
  68.     unpuppetAll()
  69.     hideSprites(gSimObject)
  70.     clearAllActors()
  71.     repeat with obstacleType in [#Rocks, #Valley, #River]
  72.       if the tileType of nextCellObject = obstacleType then
  73.         set the obstacleState of gSimObject to obstacleType
  74.         keepSandboxBut(the sandboxChan of gSimObject)
  75.         puppetPalette("SimPal")
  76.         go("Grass")
  77.         go(the frame + 1)
  78.         if obstacleType = #River then
  79.           puppetPalette("RiverPal", 60)
  80.         else
  81.           if obstacleType = #Valley then
  82.             puppetPalette("DitchPal", 60)
  83.           else
  84.             puppetPalette("DynoPal", 60)
  85.           end if
  86.         end if
  87.         updateStage()
  88.         puppetPalette(0)
  89.         go(string(obstacleType))
  90.         puppetSprite(48, 0)
  91.         exit repeat
  92.       end if
  93.     end repeat
  94.     flushBuffer()
  95.     abort()
  96.   else
  97.     blockCrossings(cell, newMove)
  98.     part2ofMove(me)
  99.     flushBuffer()
  100.   end if
  101. end
  102.  
  103. on part2ofMove me
  104.   set lastMove to newMove
  105.   goToNextCell(me)
  106. end
  107.  
  108. on goToNextCell me
  109.   if (newMove = #n) or (newMove = #s) or (newMove = #E) or (newMove = #w) then
  110.     set pixelsToMove to the cellHeight of gSimObject
  111.     set tempo to 60
  112.   else
  113.     set pixelsToMove to the cellHalfV of gSimObject
  114.     set tempo to 40
  115.   end if
  116.   set deltaList to getProp(the deltaList of gSimObject, newMove)
  117.   set Xincrement to getAt(deltaList, 1)
  118.   set Yincrement to getAt(deltaList, 2)
  119.   set status to #animating
  120.   set animCounter to 1
  121.   set animationScript to "moveLinear( gTruckObject )"
  122.   if playBack then
  123.     moveLinear(me)
  124.   end if
  125. end
  126.  
  127. on moveLinear me
  128.   if animCounter <= pixelsToMove then
  129.     set the locH of sprite channel to Xloc + (animCounter * Xincrement)
  130.     set the locV of sprite channel to Yloc + (animCounter * Yincrement)
  131.     set animCounter to animCounter + 1
  132.     puppetTempo(tempo)
  133.   else
  134.     set status to #idle
  135.     set Xloc to the Xloc of nextCellObject
  136.     set Yloc to the Yloc of nextCellObject
  137.     if the finishedFlag of gSimObject = 0 then
  138.       set connectedBuildingCell to checkForConnection()
  139.     end if
  140.     set previousCell to cell
  141.     set cell to nextCell
  142.     set cellObject to nextCellObject
  143.     if (the level of gLevelObject = 3) and (the finishedFlag of gSimObject <> 1) then
  144.       reduceGasGauge(gGasGaugeObject)
  145.       updateGasGauge(gGasGaugeObject)
  146.       updateStage()
  147.     end if
  148.     afterArrival(me, connectedBuildingCell)
  149.   end if
  150. end
  151.  
  152. on afterArrival me, connectedBuildingCell
  153.   if playBack then
  154.     set lastMove to newMove
  155.     surroundStaticSprite(gSimObject, previousCell, cell)
  156.     set the locH of sprite the underTruckSprite of gSimObject to 1000
  157.     updateStage()
  158.     set playBackCounter to playBackCounter - 1
  159.     set status to #idle
  160.     driveBack(me)
  161.   else
  162.     if programmedMove = #enterBridge then
  163.       set programmedMove to #leaveBridge
  164.       moveTruck(gTruckObject, exitBridgeDir)
  165.       set the obstacleState of gSimObject to EMPTY
  166.       go(the frame)
  167.     else
  168.       set availableMoveList to createAvailableList(gTruckObject, cell)
  169.       refreshControls(availableMoveList)
  170.       set the tileType of cellObject to #Road
  171.       if connectedBuildingCell = 0 then
  172.         set the tileNum of cellObject to getProp(the partialTileList of gSimObject, newMove)
  173.       else
  174.         redrawCell(connectedBuildingCell, the stampSprite of gSimObject)
  175.       end if
  176.       redrawCell(cellObject, the destinationSprite of gSimObject)
  177.       updateStage()
  178.       if not connectedBuildingCell = 0 then
  179.         set the buildingsConnected of gSimObject to the buildingsConnected of gSimObject + 1
  180.         playSound(buildingSound)
  181.       end if
  182.       set doneFlag to 0
  183.       repeat with adjVector in the adjacentList of cellObject
  184.         set adjCell to cell + getProp(the moveList of gSimObject, adjVector)
  185.         if adjCell = the goalCell of gSimObject then
  186.           set newMove to adjVector
  187.           add(the recordedMoveList of gSimObject, newMove)
  188.           set doneFlag to 1
  189.           exit repeat
  190.         end if
  191.       end repeat
  192.       flushBuffer()
  193.       if doneFlag = 1 then
  194.         set programmedMove to #finalRotation
  195.         rotateSprite(me, lastMove, newMove)
  196.       else
  197.         set stuckFlag to areYouStuck(cell, availableMoveList)
  198.         if stuckFlag <> 1 then
  199.           if the level of gLevelObject = 3 then
  200.             if the gasLevel of gGasGaugeObject < 1 then
  201.               hideControls()
  202.               updateStage()
  203.               set outOfGasFlag to 1
  204.               outOfGasAlert()
  205.             end if
  206.           end if
  207.           if outOfGasFlag <> 1 then
  208.             placeInvisibleButtons(gSimObject, availableMoveList, 1)
  209.           end if
  210.         end if
  211.         if programmedMove = #leaveBridge then
  212.           set programmedMove to 0
  213.           go(the frame + 1)
  214.           abort()
  215.         end if
  216.       end if
  217.     end if
  218.   end if
  219. end
  220.  
  221. on finishedRoad me
  222.   fixTile(gSimObject)
  223.   set goalObj to getAt(the cellList of gSimObject, the goalCell of gSimObject)
  224.   set oppositeDir to getProp(the convertList of gSimObject, newMove)
  225.   set the tileNum of goalObj to the tileNum of goalObj + 8 + getPos(the arrowList of gSimObject, oppositeDir)
  226.   redrawCell(goalObj, the stampSprite of gSimObject)
  227.   set the tileNum of cellObject to the roadTile1 of gSimObject + getProp(getProp(the tileList of gSimObject, lastMove), newMove)
  228.   redrawCell(cellObject, the destinationSprite of gSimObject)
  229.   puppetSound("HERE")
  230.   hideControls()
  231.   updateStage()
  232.   set lastMove to getLast(the recordedMoveList of gSimObject)
  233.   set the truck1 of gSimObject to the returnVehicle of gSimObject
  234.   preloadVehicle(me, the truck1 of gSimObject)
  235.   set the castNum of sprite channel to the truck1 of gSimObject + (5 * (getPos(the arrowList of gSimObject, lastMove) - 1))
  236.   updateStage()
  237.   repeat while soundBusy(1)
  238.   end repeat
  239.   puppetSound(0)
  240.   set playBack to 1
  241.   set playBackCounter to count(the recordedMoveList of gSimObject) - 1
  242.   driveBack(me)
  243. end
  244.  
  245. on driveBack me
  246.   if playBackCounter >= 2 then
  247.     set newMove to getAt(the recordedMoveList of gSimObject, playBackCounter)
  248.     set nextCell to cell + getProp(the moveList of gSimObject, newMove)
  249.     set nextCellObject to getAt(the cellList of gSimObject, nextCell)
  250.     surroundSprite(cell, newMove)
  251.     updateStage()
  252.     redrawCell(cellObject, the underTruckSprite of gSimObject)
  253.     redrawCell(nextCellObject, the destinationSprite of gSimObject)
  254.     updateStage()
  255.     set programmedMove to #driveBack
  256.     rotateSprite(me, lastMove, newMove)
  257.   else
  258.     set newMove to getAt(the recordedMoveList of gSimObject, 1)
  259.     surroundSprite(cell, newMove)
  260.     redrawCell(cellObject, the underTruckSprite of gSimObject)
  261.     redrawCell(getAt(the cellList of gSimObject, the startCell of gSimObject), the destinationSprite of gSimObject)
  262.     updateStage()
  263.     set programmedMove to #lastMove
  264.     rotateSprite(me, lastMove, newMove)
  265.   end if
  266. end
  267.  
  268. on fillRoadTile
  269.   if programmedMove <> #leaveBridge then
  270.     set filledRoadTile to the roadTile1 of gSimObject + getProp(getProp(the tileList of gSimObject, lastMove), newMove)
  271.     set the tileNum of cellObject to filledRoadTile + the connector of cellObject
  272.   end if
  273.   redrawCell(cellObject, the underTruckSprite of gSimObject)
  274. end
  275.  
  276. on rumbleAndShake me, lastMove, latestMove
  277.   set programmedMove to #turnToImmovable
  278.   rotateSprite(me, lastMove, latestMove)
  279. end
  280.  
  281. on rotateSprite me, oldWay, newWay
  282.   if oldWay <> newWay then
  283.     set difference to getPos(the arrowList of gSimObject, newWay) - getPos(the arrowList of gSimObject, oldWay)
  284.     set status to #animating
  285.     set currentState to the castNum of sprite channel
  286.     set animCounter to 1
  287.     if difference < 0 then
  288.       if abs(difference) > 4 then
  289.         set animationScript to "rotateClockwise( gTruckObject )"
  290.         set rotationSteps to 5 * (8 - abs(difference))
  291.       else
  292.         set animationScript to "rotateCounterclockwise( gTruckObject )"
  293.         set rotationSteps to 5 * abs(difference)
  294.       end if
  295.     else
  296.       if abs(difference) > 4 then
  297.         set animationScript to "rotateCounterclockwise( gTruckObject )"
  298.         set rotationSteps to 5 * (8 - abs(difference))
  299.       else
  300.         set animationScript to "rotateClockwise( gTruckObject )"
  301.         set rotationSteps to 5 * abs(difference)
  302.       end if
  303.     end if
  304.   else
  305.     if (the demoFlag of gSimObject = 1) and (the finishedFlag of gSimObject = 0) then
  306.       startTimer()
  307.       repeat while the timer < 5
  308.       end repeat
  309.     end if
  310.     doneRotating(me)
  311.   end if
  312. end
  313.  
  314. on rotateClockwise me
  315.   if animCounter <= rotationSteps then
  316.     set currentState to currentState + 1
  317.     if currentState = (the truck1 of gSimObject + 40) then
  318.       set currentState to the truck1 of gSimObject
  319.     end if
  320.     set the castNum of sprite channel to currentState
  321.     set animCounter to animCounter + 1
  322.     puppetTempo(40)
  323.   else
  324.     doneRotating(me)
  325.   end if
  326. end
  327.  
  328. on rotateCounterclockwise me
  329.   if animCounter <= rotationSteps then
  330.     set currentState to currentState - 1
  331.     if currentState < the truck1 of gSimObject then
  332.       set currentState to the truck1 of gSimObject + 39
  333.     end if
  334.     set the castNum of sprite channel to currentState
  335.     set animCounter to animCounter + 1
  336.     puppetTempo(40)
  337.   else
  338.     doneRotating(me)
  339.   end if
  340. end
  341.  
  342. on doneRotating me
  343.   if (programmedMove = 0) or (programmedMove = #leaveBridge) then
  344.     afterRotating(me)
  345.   else
  346.     if programmedMove = #driveBack then
  347.       goToNextCell(me)
  348.     else
  349.       if programmedMove = #turnToImmovable then
  350.         sound playFile 1, gCDpath & "SOUND" & gFileSep & "SIM" & gFileSep & "OOPS" & string(random(5))
  351.         set programmedMove to #awayFromImmovable
  352.         rotateSprite(me, latestMove, lastMove)
  353.         repeat while soundBusy(1)
  354.         end repeat
  355.       else
  356.         if programmedMove = #awayFromImmovable then
  357.           replaceInvisibleButtons(gSimObject)
  358.           set status to #idle
  359.           set programmedMove to 0
  360.         else
  361.           if programmedMove = #finalRotation then
  362.             set the finishedFlag of gSimObject to 1
  363.             repeat with x = 1 to count(the recordedMoveList of gSimObject)
  364.               setAt(the recordedMoveList of gSimObject, x, getProp(the convertList of gSimObject, getAt(the recordedMoveList of gSimObject, x)))
  365.             end repeat
  366.             set programmedMove to 0
  367.             finishedRoad(me)
  368.           else
  369.             if programmedMove = #lastMove then
  370.               isRoadFinished(gSimObject)
  371.             end if
  372.           end if
  373.         end if
  374.       end if
  375.     end if
  376.   end if
  377. end
  378.  
  379. on preloadVehicle me, firstCast
  380.   preLoadCast(firstCast, firstCast + 39)
  381. end
  382.  
  383. on checkForConnection
  384.   set connectedBuildingCell to 0
  385.   repeat with adjVector in the adjacentList of nextCellObject
  386.     set adjacentCell to getAt(the cellList of gSimObject, nextCell + getProp(the moveList of gSimObject, adjVector))
  387.     if the tileType of adjacentCell = #Building then
  388.       if the connector of adjacentCell = 0 then
  389.         set tileName to stripSuffix(the name of cast the tileNum of adjacentCell)
  390.         set buildingSound to tileName & "Sound"
  391.         set connectedBuilding to the tileNum of adjacentCell + getPos(the arrowList of gSimObject, adjVector)
  392.         set the tileNum of adjacentCell to connectedBuilding
  393.         set the connector of adjacentCell to 1
  394.         set roadConnector to getPos(the arrowList of gSimObject, getProp(the convertList of gSimObject, adjVector))
  395.         set the tileNum of nextCellObject to getProp(the partialTileList of gSimObject, newMove) + roadConnector
  396.         set the connector of nextCellObject to roadConnector
  397.         set x to blockCrossings(nextCell, adjVector)
  398.         set connectedBuildingCell to adjacentCell
  399.         exit repeat
  400.       end if
  401.     end if
  402.   end repeat
  403.   return connectedBuildingCell
  404. end
  405.  
  406. on createAvailableList me, cell
  407.   set adjacentCellList to the adjacentList of getAt(the cellList of gSimObject, cell)
  408.   set availableMoveList to []
  409.   repeat with x = 1 to count(adjacentCellList)
  410.     add(availableMoveList, getAt(adjacentCellList, x))
  411.   end repeat
  412.   set filledCellList to []
  413.   repeat with x = 1 to count(availableMoveList)
  414.     set adjVector to getAt(availableMoveList, x)
  415.     set adjCell to cell + getProp(the moveList of gSimObject, adjVector)
  416.     set adjObj to getAt(the cellList of gSimObject, adjCell)
  417.     if (the tileType of adjObj = #Road) or (the tileType of adjObj = #Bridge) or (the tileType of adjObj = #Goal) or (the tileType of adjObj = #Building) then
  418.       add(filledCellList, adjVector)
  419.       next repeat
  420.     end if
  421.     if the tileType of adjObj = #River then
  422.       if checkBlockage(adjCell, adjObj, adjVector) = 1 then
  423.         add(filledCellList, adjVector)
  424.       end if
  425.     end if
  426.   end repeat
  427.   repeat with x = 1 to count(filledCellList)
  428.     set filledCell to getAt(filledCellList, x)
  429.     set position to getPos(availableMoveList, filledCell)
  430.     if position <> 0 then
  431.       deleteAt(availableMoveList, position)
  432.     end if
  433.   end repeat
  434.   return availableMoveList
  435. end
  436.  
  437. on drawTruck me
  438.   repeat with x = 1 to count(the adjacentList of cellObject)
  439.     set adjCell to getProp(the moveList of gSimObject, getAt(the adjacentList of cellObject, x))
  440.     redrawCell(getAt(the cellList of gSimObject, cell + adjCell), the firstSurroundSprite of gSimObject + x - 1)
  441.   end repeat
  442.   updateStage()
  443.   redrawCell(cellObject, the destinationSprite of gSimObject)
  444.   set the castNum of sprite channel to the truck1 of gSimObject + (5 * (getPos(the arrowList of gSimObject, newMove) - 1))
  445.   set the locH of sprite channel to the Xloc of cellObject
  446.   set the locV of sprite channel to the Yloc of cellObject
  447.   updateStage()
  448. end
  449.  
  450. on restore me
  451.   set the locH of sprite channel to the locH of sprite channel - 1000
  452. end
  453.  
  454. on makeBridge me
  455.   set riverTileName to char 1 of the name of cast the tileNum of blockedCellObj
  456.   set bridgeData to getProp(getProp(the bridgeList of gSimObject, riverTileName), newMove)
  457.   set the tileNum of nextCellObject to the bridge1 of gSimObject + getAt(bridgeData, 1)
  458.   set the tileType of nextCellObject to #Bridge
  459.   set exitBridgeDir to getAt(bridgeData, 2)
  460. end
  461.  
  462. on loadTruck me, index
  463.   set cell to value(item index of the dataString of gMenuObject)
  464.   set index to index + 1
  465.   set nextCell to value(item index of the dataString of gMenuObject)
  466.   set index to index + 1
  467.   set firstCell to value(item index of the dataString of gMenuObject)
  468.   set index to index + 1
  469.   set previousCell to value(item index of the dataString of gMenuObject)
  470.   set index to index + 1
  471.   set newMove to getAt(the arrowList of gSimObject, value(item index of the dataString of gMenuObject))
  472.   set index to index + 1
  473.   set lastMove to getAt(the arrowList of gSimObject, value(item index of the dataString of gMenuObject))
  474.   set index to index + 1
  475.   set startDirection to getAt(the arrowList of gSimObject, value(item index of the dataString of gMenuObject))
  476.   set index to index + 1
  477.   set cellObject to getAt(the cellList of gSimObject, cell)
  478.   set Xloc to the Xloc of cellObject
  479.   set Yloc to the Yloc of cellObject
  480.   set nextCellObject to getAt(the cellList of gSimObject, nextCell)
  481.   return index
  482. end
  483.  
  484. on saveTruck me
  485.   repeat with propName in [cell, nextCell, firstCell, previousCell]
  486.     set truckData to truckData & string(propName) & ","
  487.   end repeat
  488.   repeat with propName in [newMove, lastMove, startDirection]
  489.     set truckData to truckData & string(getPos(the arrowList of gSimObject, propName)) & ","
  490.   end repeat
  491.   return truckData
  492. end
  493.